package com.project.yunshitong;

import android.app.Application;
import android.os.Environment;
import android.util.Log;

import com.project.yunshitong.utils.CrashHandler;
import com.project.yunshitong.utils.FileSaveRFID;
import com.project.yunshitong.utils.MySQLiteOpenHelper;
import com.tencent.bugly.crashreport.CrashReport;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;


public class MyApplication extends Application implements Thread.UncaughtExceptionHandler{

    MySQLiteOpenHelper dbHelper;

    @Override
    public void onCreate() {
        super.onCreate();
//        CrashReport.initCrashReport(this,"4e6b28be20",false);
        dbHelper = new MySQLiteOpenHelper(this, "goods_info");
        //数据库实际上是没有被创建或者打开的，直到getWritableDatabase() 或者 getReadableDatabase() 方法中的一个被调用时才会进行创建或者打开
        dbHelper.getWritableDatabase();
        CrashHandler crashHandler = CrashHandler.getInstance();
        crashHandler.init(this);
    }


    @Override
    public void uncaughtException(Thread thread, Throwable exception) {
        String stackTrace = Log.getStackTraceString(exception);
        Log.e("TAG", "uncaughtException: 退信息==-------"+stackTrace );
        File crashLog = new File(Environment.getExternalStorageDirectory() + "/crash_" + System.currentTimeMillis() + ".txt");
        File logDir = crashLog.getParentFile();
        if (!logDir.exists()) {
            logDir.mkdirs();
        }

        try {
            PrintWriter writer = new PrintWriter(crashLog);
            writer.println(stackTrace);
            writer.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        System.exit(1);
    }
}
